//
//  UIView+category.m
//  deerkids
//
//  Created by 王昭的Mac Air on 2017/8/28.
//  Copyright © 2017年 wangzhao. All rights reserved.
//

#import "UIView+category.h"

@implementation UIView (category)

-(CGFloat)x
{
    return self.frame.origin.x;
}

-(void)setX:(CGFloat)x
{
    self.frame = CGRectMake(x, self.y, self.width, self.height);
}

-(CGFloat)y
{
    return self.frame.origin.y;
}

-(void)setY:(CGFloat)y
{
    self.frame = CGRectMake(self.x, y, self.width, self.height);
}

-(CGFloat)width
{
    return self.frame.size.width;
}

-(void)setWidth:(CGFloat)width
{
    self.frame = CGRectMake(self.x, self.y, width, self.height);
}

-(CGFloat)height
{
    return  self.frame.size.height;
}

-(void)setHeight:(CGFloat)height
{
    self.frame = CGRectMake(self.x, self.y, self.width, height);
}

- (CGFloat)centerX
{
    return self.center.x;
}

- (void)setCenterX:(CGFloat)centerX
{
    CGPoint center = self.center;
    center.x = centerX;
    self.center = center;
}

- (CGFloat)centerY
{
    return self.center.y;
}

- (void)setCenterY:(CGFloat)centerY
{
    CGPoint center = self.center;
    center.y = centerY;
    self.center = center;
}

#pragma mark - 获取当前view所在的控制器
- (UIViewController *)getCurrentController
{
    for (UIView *next = self; next; next = next.superview) {
        UIResponder *nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)nextResponder;
        }
    }
    return nil;
}

#pragma mark - 手势
//单击
- (void)addTapGestureRecognizerWithTarget:(id)target action:(SEL)sel
{
    self.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:target action:sel];
    [self addGestureRecognizer:tapGR];
}

//长按
- (void)addLongGestureRecognizerWithTarget:(id)target action:(SEL)sel
{
    self.userInteractionEnabled = YES;
    UILongPressGestureRecognizer *longGR = [[UILongPressGestureRecognizer alloc] initWithTarget:target action:sel];
    [self addGestureRecognizer:longGR];
}

//拖拽
- (void)addPanGestureRecognizerWithTarget:(id)target action:(SEL)sel
{
    self.userInteractionEnabled = YES;
    UIPanGestureRecognizer *panGR = [[UIPanGestureRecognizer alloc] initWithTarget:target action:sel];
    [self addGestureRecognizer:panGR];
}

//左滑
- (void)addLeftSwipeGestureRecognizerWithTarget:(id)target action:(SEL)sel
{
    self.userInteractionEnabled = YES;
    UISwipeGestureRecognizer *swipeGR = [[UISwipeGestureRecognizer alloc] initWithTarget:target action:sel];
    swipeGR.direction = UISwipeGestureRecognizerDirectionLeft;
    [self addGestureRecognizer:swipeGR];
}

//右滑
- (void)addRightSwipeGestureRecognizerWithTarget:(id)target action:(SEL)sel
{
    self.userInteractionEnabled = YES;
    UISwipeGestureRecognizer *swipeGR = [[UISwipeGestureRecognizer alloc] initWithTarget:target action:sel];
    swipeGR.direction = UISwipeGestureRecognizerDirectionRight;
    [self addGestureRecognizer:swipeGR];
}

//上滑
- (void)addUpSwipeGestureRecognizerWithTarget:(id)target action:(SEL)sel
{
    self.userInteractionEnabled = YES;
    UISwipeGestureRecognizer *swipeGR = [[UISwipeGestureRecognizer alloc] initWithTarget:target action:sel];
    swipeGR.direction = UISwipeGestureRecognizerDirectionUp;
    [self addGestureRecognizer:swipeGR];
}

//下滑
- (void)addDownSwipeGestureRecognizerWithTarget:(id)target action:(SEL)sel
{
    self.userInteractionEnabled = YES;
    UISwipeGestureRecognizer *swipeGR = [[UISwipeGestureRecognizer alloc] initWithTarget:target action:sel];
    swipeGR.direction = UISwipeGestureRecognizerDirectionDown;
    [self addGestureRecognizer:swipeGR];
}



- (void)removeAllGestureRecognizer
{
    for (UITapGestureRecognizer *tap in self.gestureRecognizers) {
        [self removeGestureRecognizer:tap];
    }
}


@end
